<?php

namespace app\bh\controller;

use app\bh\model\DataModel;
use app\bh\model\IconModel;
use think\Controller;
use think\Request;

class IconController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $icons = IconModel::select()->toArray();

        return $this->SuccessReturn('ok', $icons);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {

        //
        $icon = request()->file();
        $user_path = '../public/icon/user' . self::$user['id'];

        $info = $icon['icon']->move($user_path);
        $sername = $request->server('REQUEST_SCHEME') . '://' . $request->server('SERVER_NAME');
        if ($info) {
            // 输出 jpg
//            return $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $name = $info->getSaveName();
            $savename = $sername . "/icon/user" . self::$user['id'] . "/" . $name;

            $iconmodel = new IconModel();
            $iconmodel->iconurl = $savename;
            $iconmodel->uid = self::$user['id'];
            $iconmodel->save();
            return $this->SuccessReturn('ok', $iconmodel);
        }
        return $this->ErrorReturn(400, "上传失败");
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $id = $request->put("id");
        dump($id);
        if ($id == '') return $this->ErrorReturn(403, '缺少id');
        $icon = request()->file();
        $user_path = '../public/icon/user' . self::$user['id'];
        $info = $icon['icon']->move($user_path);
        $iconmodel = IconModel::find($id);
        if (!$iconmodel) return $this->ErrorReturn(400, '没找到icon对象');
        if ($info) {
            // 输出 jpg
//            return $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $name = $info->getSaveName();
            $savename = "/icon/user" . self::$user['id'] . "/" . $name;
            $iconmodel->iconurl = $savename;
            $iconmodel->save();
            return $this->SuccessReturn('ok', $iconmodel);
        }
        return $this->ErrorReturn(400, "修改失败");
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        $id = $request->delete('id');
        if ($id == 0) return $this->ErrorReturn(400, '图标id无效');
        //查询是否存在此用户
        $icon = IconModel::where('id', $id)->find();
        if (!$icon) return $this->ErrorReturn(404, '找不到此区域');
        //删除文件

        $name_arr = explode('/icon', $icon->iconurl);
        $url = iconv('utf-8', 'gbk', '../public/icon' . $name_arr[1]);
        unlink($url);
        $icon->delete();
        return $this->SuccessReturn('删除成功');
    }
}
